Conversion to string: how do `s work?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Lasher

    #1

    Conversion to string: how do `s work?

    I'm working my way through _Learning_Pytho n_ 2nd ed., and I saw
    something peculiar which is not explained anywhere in the text.

    print " " + file + " size=" + `size`

    The `s appear to somehow automagically convert the integer to a string
    for concatenation. How does this work? Is this just a shortcut for
    str(size)? Is it considered bad practice to use `s?

  • Michael Hoffman

    #2
    Re: Conversion to string: how do `s work?

    Chris Lasher wrote:
    [color=blue]
    > The `s appear to somehow automagically convert the integer to a string
    > for concatenation. How does this work? Is this just a shortcut for
    > str(size)?[/color]

    No, it's a shortcut for repr(size).
    [color=blue]
    > Is it considered bad practice to use `s?[/color]

    Yes, please don't do it. I can't remember ever seeing it in production
    code, so many people wouldn't even know what it was. And ' vs. ` is
    confusing.
    --
    Michael Hoffman

    Comment

    • Chris Lasher

      #3
      Re: Conversion to string: how do `s work?

      Ah, repr. Did not cross my mind. Good to see my suspicions about the
      use of ` being poor practice were correct. "Explicit is better than
      implicit."

      Thanks for the reply.

      Comment

      Working...